<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<style type="text/css">
	.right-operation ul{ list-style: none; }
	.right-operation ul li{ margin-top:10px;margin-bottom:10px; }
	.inputbtn{ height:35px;width:90px; }
	.td1{width:60%;}
	.td2{width:20%;}
	.td3{width:20%;}
</style>

<div class="right-panel">
	<div class="header">
		<h1>上传页面页面</h1>
	</div>
	
	<div class="right-operation">
		<h2>上传编辑</h2>
		<form action="/admin_t/upload" enctype="multipart/form-data" method="post">
			<table>
			<tr>
			<input type="hidden" name="fileName" value="" id="key-name"/>
			<td class="td1"><input type="file" name="file" id="key-file"/></td>
			<td class="td2"><input type="submit" value="增加" class="inputbtn" id="file-submit"/></td>
			<td class="td3"></td>
			</tr>
			</table>
		</form>

		<table>
            <thead>
                <tr>
                    <td class="td1">文件名</td>
                    <td class="td2">操作</td>
                    <td class="td3">操作</td>
                </tr>
            </thead>
            <tbody>
				<c:forEach items="${data}" var="a" varStatus="s">
				<tr>
					<td class="td1"><input type="text" id="key${s.count}" value="${a.fileName}" data-id="${a._id }" disabled="disabled"/></td>
					<td class="td2"><input type="button" value="查看" class="inputbtn" data-ops="go" data-index="http://${server}/upload/${a.filePath}"/></td>
					<td class="td3"><input type="button" value="删除" class="inputbtn" data-ops="delete" data-index="${s.count}"/></td>
				</tr>
				</c:forEach>
			</tbody>
		</table>
	</div>
	
</div>
<script type="text/javascript">
	(function(){
		var _ops = function(ops,index){
			var k = $("#key" + index).val();
			var v = $("#key" + index).attr("data-id");
			var url = "/admin_t/operation?p=upload&t=" + ops;
			$.ajax({
				url:url,
				cache:false,
				data:"k=" + encodeURIComponent(k) + "&v=" + encodeURIComponent(v),
				dataType:"json",
				success:function(json){
					if( json && json.code == 200 ){
						window.location.href = "http://${server}/admin_t/index?p=upload";
					}else{
						alert("操作失败,错误代码:" + json.msg);
					}
				},
				error:function(){
					alert("操作失败!");
				}
			});
		}
		
		var init = function(){
			$("#file-submit").click(function(){
				var v = $("#key-file").val();
				if( !v || v.length == 0 ){
					alert("请先选择上传文件!");
					return false;
				}
				$("#key-name").attr("value",v);
				return true;
			});
			$(".inputbtn").each(function(){
				var t = $(this);
				var ops = t.attr("data-ops");
				var index = t.attr("data-index");
				if( !ops || ops.length == 0 || !index || index.length == 0){
					return ;
				}
				if( "go" == ops ){
					t.click(function(){
						window.location.href = index;
					});
					return ;
				}
				t.click(function(){
					_ops(ops,index);
				});
			});
		}
		init();
	})();
</script>